package com.wlc.controller;

import com.wlc.pojo.LeaveStats;
import com.wlc.pojo.Result;
import com.wlc.service.LeaveStatsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/stats/leave")
@Slf4j
public class LeaveStatsController {

    @Autowired
    private LeaveStatsService leaveStatsService;

    @GetMapping("/dept")
    public Result<List<LeaveStats>> getDeptStats(@RequestParam(required = false) String startDate,
                                                 @RequestParam(required = false) String endDate) {
        log.info("获取部门请假统计: startDate={}, endDate={}", startDate, endDate);
        try {
            List<LeaveStats> stats = leaveStatsService.getDeptLeaveStats(startDate, endDate);
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取部门请假统计失败: {}", e.getMessage());
            return Result.error("获取部门请假统计失败");
        }
    }

    @GetMapping("/type")
    public Result<List<LeaveStats>> getTypeStats(@RequestParam(required = false) String startDate,
                                                 @RequestParam(required = false) String endDate) {
        log.info("获取请假类型统计: startDate={}, endDate={}", startDate, endDate);
        try {
            List<LeaveStats> stats = leaveStatsService.getLeaveTypeStats(startDate, endDate);
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取请假类型统计失败: {}", e.getMessage());
            return Result.error("获取请假类型统计失败");
        }
    }

    @GetMapping("/monthly-trend")
    public Result<List<LeaveStats>> getMonthlyTrend(@RequestParam(required = false) String year) {
        log.info("获取月度请假趋势: year={}", year);
        try {
            List<LeaveStats> trend = leaveStatsService.getMonthlyLeaveTrend(year);
            return Result.success(trend);
        } catch (Exception e) {
            log.error("获取月度请假趋势失败: {}", e.getMessage());
            return Result.error("获取月度请假趋势失败");
        }
    }

    @GetMapping("/personal")
    public Result<List<LeaveStats>> getPersonalStats(@RequestParam(required = false) String startDate,
                                                     @RequestParam(required = false) String endDate) {
        log.info("获取个人请假统计: startDate={}, endDate={}", startDate, endDate);
        try {
            List<LeaveStats> stats = leaveStatsService.getPersonalLeaveStats(startDate, endDate);
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取个人请假统计失败: {}", e.getMessage());
            return Result.error("获取个人请假统计失败");
        }
    }

    @GetMapping("/status")
    public Result<List<LeaveStats>> getStatusStats(@RequestParam(required = false) String startDate,
                                                   @RequestParam(required = false) String endDate) {
        log.info("获取请假审批状态统计: startDate={}, endDate={}", startDate, endDate);
        try {
            List<LeaveStats> stats = leaveStatsService.getLeaveStatusStats(startDate, endDate);
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取请假审批状态统计失败: {}", e.getMessage());
            return Result.error("获取请假审批状态统计失败");
        }
    }

    @GetMapping("/overview")
    public Result<Object> getOverview(@RequestParam(required = false) String startDate,
                                      @RequestParam(required = false) String endDate) {
        log.info("获取请假统计总览: startDate={}, endDate={}", startDate, endDate);
        try {
            Object overview = leaveStatsService.getLeaveOverview(startDate, endDate);
            return Result.success(overview);
        } catch (Exception e) {
            log.error("获取请假统计总览失败: {}", e.getMessage());
            return Result.error("获取请假统计总览失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> deleteLeave(@PathVariable Long id) {
        log.info("删除请假记录: id={}", id);
        try {
            boolean success = leaveStatsService.deleteLeaveById(id);
            return success ? Result.success(true) : Result.error("删除失败");
        } catch (Exception e) {
            log.error("删除请假记录失败: {}", e.getMessage());
            return Result.error("删除失败");
        }
    }

    @DeleteMapping("/employee/{employeeId}")
    public Result<Boolean> deleteLeaveByEmployee(@PathVariable Long employeeId) {
        log.info("删除员工请假记录: employeeId={}", employeeId);
        try {
            boolean success = leaveStatsService.deleteLeaveByEmployeeId(employeeId);
            return success ? Result.success(true) : Result.error("删除失败");
        } catch (Exception e) {
            log.error("删除员工请假记录失败: {}", e.getMessage());
            return Result.error("删除失败");
        }
    }

    @PostMapping("/batch-delete")
    public Result<Boolean> batchDeleteLeave(@RequestBody List<Long> ids) {
        log.info("批量删除请假记录: ids={}", ids);
        try {
            boolean success = leaveStatsService.batchDeleteLeave(ids);
            return success ? Result.success(true) : Result.error("批量删除失败");
        } catch (Exception e) {
            log.error("批量删除请假记录失败: {}", e.getMessage());
            return Result.error("批量删除失败");
        }
    }
}